// pages/shoppingcart/shoppingcart.js
import util from "../../utils/util"
import Api from "../../utils/api"
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hasLogin: false,
        cartGoods: [],
        cartTotal: {
            "goodsCount": 0,
            "goodsAmount": 0.00,
            "checkedGoodsCount": 0,
            "checkedGoodsAmount": 0.00
        },
        isEditCart: false,
        checkedAllStatus: true,
        editCartList: []
    },
    stepperOnChange(event){
        let that = this;
        let itemIndex = event.target.dataset.itemIndex;
        let detail = event.detail;
        let total = 0;
        let tmpCartData = this.data.cartGoods.map(function (v, index) {
            if(v.checked){
                if(index == itemIndex){
                    total = total + v.price * 100 * detail;
                }else{
                    total = total + v.price * 100 * v.number;
                }
            }
            if(index == itemIndex){
                v.number = detail;
            }
            return v;
        });
        that.setData({
            cartGoods: tmpCartData,
            'cartTotal.checkedGoodsAmount': total,
        });
    },
    onChange(event) {
        let that = this;
        let itemIndex = event.target.dataset.itemIndex;
        let totle = that.data.cartTotal.checkedGoodsAmount;
        let tmpCartData = this.data.cartGoods.map(function (element, index, array) {
            if (index == itemIndex) {
                element.checked = !element.checked;
                if(element.checked){
                    totle = totle + element.price * 100 * element.number;
                }else{
                    totle = totle - element.price * 100 * element.number;
                }
            }
            return element;
        });
        that.setData({
            cartGoods: tmpCartData,
            'cartTotal.checkedGoodsAmount': totle,
            checkedAllStatus: that.isCheckedAll()
        });
    },
    checkedAll: function () {
        let that = this;
        let checkedAllStatus = that.isCheckedAll();
        let total = 0;
        let tmpCartData = this.data.cartGoods.map(function (v) {
            v.checked = !checkedAllStatus;
            if(!checkedAllStatus){
                total = total + v.price * 100 * v.number;
            }
            return v;
        });
        that.setData({
            cartGoods: tmpCartData,
            checkedAllStatus: that.isCheckedAll(),
            'cartTotal.checkedGoodsAmount': total,
        });

    },
    isCheckedAll: function () {
        //判断购物车商品已全选
        return this.data.cartGoods.every(function (element, index, array) {
            if (element.checked == true) {
                return true;
            } else {
                return false;
            }
        });
    },
    onClickButton: function () {

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        
    },
    goLogin() {
        wx.navigateTo({
            url: "/pages/auth/login/login"
        });
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if (app.globalData.hasLogin) {
            this.getCartList();
        }
        this.setData({
            hasLogin: app.globalData.hasLogin
        });
        this.data.checkedAllStatus = this.isCheckedAll();
    },
    getCartList: function () {
        let that = this;
        util.request(Api.wx.CartList).then(function (res) {
            if (res.errno === 0) {
                that.setData({
                    cartGoods: res.data.cartList,
                    cartTotal: res.data.cartTotal
                });
            }
        });
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        wx.showNavigationBarLoading() //在标题栏中显示加载
        this.getCartList();
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})